package GUIs;

import Configs.Config;
import Controllers.GeneratetPDF;
import Controllers.MyTableFormat;
import Controllers.NhanVienController_2;
import com.db4o.ObjectSet;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.text.ParseException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import models.NhanVien;
import javax.swing.JScrollPane;

public class f_quanly extends javax.swing.JFrame {

    private final Config conn;
    final NhanVienController_2 nvcontrol;
    private final NhanVien nvdangnhap;
    private MyTableFormat myFormat;
    private GeneratetPDF genPDF;

    public MyTableFormat getMyFormat() {
        return this.myFormat;
    }

    public NhanVienController_2 gettkcontrol() {
        return this.nvcontrol;
    }

    public javax.swing.JTable getTbnv() {
        return this.tbdsnv;
    }

    public f_quanly(Config conn, String msnv) {
        this.conn = conn;
        this.nvcontrol = new NhanVienController_2(this.conn);
        this.nvdangnhap = this.nvcontrol.getWithMSNV(msnv);
        myFormat = new MyTableFormat();
        initComponents();
        // 
        ImageIcon icon = new ImageIcon(getClass().getResource("../img/house.png"));
        Image image = icon.getImage();
        setIconImage(image);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        popupmenu = new javax.swing.JPopupMenu();
        itemsua_thongtin_nhanvien = new javax.swing.JMenuItem();
        itemxoa_thongtin_nhanvien = new javax.swing.JMenuItem();
        itemthem_tai_khoan = new javax.swing.JMenuItem();
        pnchinh = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        spdsnv = new javax.swing.JScrollPane();
        tbdsnv = new javax.swing.JTable();
        tftim = new javax.swing.JTextField();
        btntim = new javax.swing.JButton();
        btnthem = new javax.swing.JButton();
        btnrefresh = new javax.swing.JButton();
        btnnhanvien_isdelete = new javax.swing.JButton();
        lbtenbang = new javax.swing.JLabel();
        menu = new javax.swing.JMenuBar();
        menuhethong = new javax.swing.JMenu();
        itemthongtin = new javax.swing.JMenuItem();
        itemdangxuat = new javax.swing.JMenuItem();
        itemthoat = new javax.swing.JMenuItem();
        menuquanly = new javax.swing.JMenu();
        itemthongke = new javax.swing.JMenuItem();
        itemqlTK = new javax.swing.JMenuItem();
        menuttuser = new javax.swing.JMenu();

        itemsua_thongtin_nhanvien.setText("Sửa thông tin nhân vien");
        itemsua_thongtin_nhanvien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemsua_thongtin_nhanvienActionPerformed(evt);
            }
        });
        popupmenu.add(itemsua_thongtin_nhanvien);

        itemxoa_thongtin_nhanvien.setText("Xóa nhân viên");
        itemxoa_thongtin_nhanvien.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemxoa_thongtin_nhanvienActionPerformed(evt);
            }
        });
        popupmenu.add(itemxoa_thongtin_nhanvien);

        itemthem_tai_khoan.setText("Thêm Tài khoản cho nhân viên này.");
        itemthem_tai_khoan.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemthem_tai_khoanActionPerformed(evt);
            }
        });
        popupmenu.add(itemthem_tai_khoan);

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Quản lý nhân viên");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowOpened(java.awt.event.WindowEvent evt) {
                formWindowOpened(evt);
            }
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setText("Quản Lý Nhân Viên và Tài Khoản");

        javax.swing.GroupLayout pnchinhLayout = new javax.swing.GroupLayout(pnchinh);
        pnchinh.setLayout(pnchinhLayout);
        pnchinhLayout.setHorizontalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addGap(174, 174, 174)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 416, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnchinhLayout.setVerticalGroup(
            pnchinhLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnchinhLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addContainerGap(20, Short.MAX_VALUE))
        );

        tbdsnv.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "STT", "MSNV", "Ho Tên", "Giới Tính"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tbdsnv.setComponentPopupMenu(popupmenu);
        tbdsnv.getTableHeader().setReorderingAllowed(false);
        tbdsnv.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbdsnvMouseClicked(evt);
            }
        });
        spdsnv.setViewportView(tbdsnv);
        if (tbdsnv.getColumnModel().getColumnCount() > 0) {
            tbdsnv.getColumnModel().getColumn(0).setResizable(false);
            tbdsnv.getColumnModel().getColumn(1).setResizable(false);
            tbdsnv.getColumnModel().getColumn(2).setResizable(false);
            tbdsnv.getColumnModel().getColumn(3).setResizable(false);
        }
        tbdsnv.getAccessibleContext().setAccessibleName("");
        tbdsnv.getAccessibleContext().setAccessibleDescription("");

        tftim.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                tftimKeyPressed(evt);
            }
        });

        btntim.setText("Tìm Nhân Viên");
        btntim.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btntimActionPerformed(evt);
            }
        });

        btnthem.setText("Thêm Nhân Viên");
        btnthem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthemActionPerformed(evt);
            }
        });

        btnrefresh.setText("Làm mới hiển thị");
        btnrefresh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnrefreshActionPerformed(evt);
            }
        });

        btnnhanvien_isdelete.setText("Xem Nhân Viên Đã Xóa");
        btnnhanvien_isdelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnhanvien_isdeleteActionPerformed(evt);
            }
        });

        lbtenbang.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        lbtenbang.setText("         ");

        menuhethong.setText("Hệ thống");

        itemthongtin.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_I, java.awt.event.InputEvent.ALT_MASK));
        itemthongtin.setText("Thông tin phần mềm");
        itemthongtin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemthongtinActionPerformed(evt);
            }
        });
        menuhethong.add(itemthongtin);

        itemdangxuat.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK | java.awt.event.InputEvent.SHIFT_MASK));
        itemdangxuat.setText("Đăng xuất");
        itemdangxuat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemdangxuatActionPerformed(evt);
            }
        });
        menuhethong.add(itemdangxuat);

        itemthoat.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F4, java.awt.event.InputEvent.ALT_MASK));
        itemthoat.setText("Thoát chương trình");
        itemthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemthoatActionPerformed(evt);
            }
        });
        menuhethong.add(itemthoat);

        menu.add(menuhethong);

        menuquanly.setText("Quản Lý");

        itemthongke.setText("Thống Kê");
        itemthongke.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemthongkeActionPerformed(evt);
            }
        });
        menuquanly.add(itemthongke);

        itemqlTK.setText("Quản lí tài khoản");
        itemqlTK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                itemqlTKActionPerformed(evt);
            }
        });
        menuquanly.add(itemqlTK);

        menu.add(menuquanly);

        menuttuser.setText("Thông tin người dùng");
        menuttuser.setFont(new java.awt.Font("Segoe UI", 1, 15)); // NOI18N
        menu.add(menuttuser);

        setJMenuBar(menu);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnchinh, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(tftim, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btntim, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnthem)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnrefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnnhanvien_isdelete, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(spdsnv)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(lbtenbang, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnchinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tftim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btntim)
                    .addComponent(btnthem)
                    .addComponent(btnrefresh, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnnhanvien_isdelete))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbtenbang)
                .addGap(11, 11, 11)
                .addComponent(spdsnv, javax.swing.GroupLayout.PREFERRED_SIZE, 307, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(42, 42, 42))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btntimActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btntimActionPerformed
        if (tftim.getText().equals("")) {
            JOptionPane.showMessageDialog(this, "Xin nhập tên hoặc Msnv để tìm kiếm!");
            tftim.requestFocus();
        } else {
            String tim = tftim.getText();
            ObjectSet<NhanVien> nvs = this.nvcontrol.getWithHoTen_MSNV(tim);
            if (!nvs.isEmpty()) {
                myFormat.clearTable(this.tbdsnv);
                nvcontrol.showTimkiem(tbdsnv, nvs);
            }
            else
                JOptionPane.showMessageDialog(this, "Không tìm thấy thông tin");
            tftim.setText("");
        }
    }//GEN-LAST:event_btntimActionPerformed

    private void btnthemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthemActionPerformed
        f_nhanvien_them themnv = f_nhanvien_them.getInstance(nvdangnhap, conn, this);
        themnv.setVisible(true);
    }//GEN-LAST:event_btnthemActionPerformed

    private void itemthongtinActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemthongtinActionPerformed
        JOptionPane.showMessageDialog(this, "Nhóm phát triển Chuột.\nPhiên bản 0.1.", "Thông tin phần mềm", JOptionPane.INFORMATION_MESSAGE);
    }//GEN-LAST:event_itemthongtinActionPerformed

    private void itemdangxuatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemdangxuatActionPerformed
        f_dangnhap dangnhap = f_dangnhap.getInstance(this.conn);
        dangnhap.createAndShowUI();
        this.dispose();
    }//GEN-LAST:event_itemdangxuatActionPerformed

    private void itemthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemthoatActionPerformed
        this.conn.closeConn();
        this.dispose();
    }//GEN-LAST:event_itemthoatActionPerformed

    private void tftimKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tftimKeyPressed
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            this.search();
        }
    }//GEN-LAST:event_tftimKeyPressed

    private void itemsua_thongtin_nhanvienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemsua_thongtin_nhanvienActionPerformed
        int row = tbdsnv.getSelectedRow();
        if (row != -1) {
            String MSNV = tbdsnv.getValueAt(row, 1).toString();
            NhanVien nv = this.nvcontrol.getWithMSNV(MSNV);
            f_nhanvien_sua f = new f_nhanvien_sua(nv, conn, this);
            f.setVisible(true);
        }
        tbdsnv.clearSelection();
    }//GEN-LAST:event_itemsua_thongtin_nhanvienActionPerformed

    private void itemxoa_thongtin_nhanvienActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemxoa_thongtin_nhanvienActionPerformed
        int check = JOptionPane.showConfirmDialog(this, "Bạn có muốn xóa tài khoản này", "Warning", JOptionPane.YES_NO_OPTION);
        if (check == JOptionPane.YES_OPTION) {
            int row = tbdsnv.getSelectedRow();
            if (row != -1) {
                String MSNV = tbdsnv.getValueAt(row, 1).toString();
                NhanVien nv = this.nvcontrol.getWithMSNV(MSNV);
                if(nv.getIsDel()){
                    JOptionPane.showMessageDialog(this, "Nhân viên này đã được xóa");
                }
                else{
                this.nvcontrol.deleteNhanVien(nv);
                JOptionPane.showMessageDialog(this, "Đã xóa Nhân Viên Thành Công!");
                myFormat.clearTable(this.tbdsnv);
                this.nvcontrol.showAllNhanVien(tbdsnv);
                }

            }
        }


    }//GEN-LAST:event_itemxoa_thongtin_nhanvienActionPerformed

    private void btnrefreshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnrefreshActionPerformed
        myFormat.clearTable(this.tbdsnv);
        lbtenbang.setText("Danh sách nhân viên");
        this.nvcontrol.showAllNhanVien(tbdsnv);

    }//GEN-LAST:event_btnrefreshActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        this.conn.closeConn();
    }//GEN-LAST:event_formWindowClosing

    private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowOpened
        menuttuser.setText("Xin chào, (" + this.nvdangnhap.getMSNV() + ") " + this.nvdangnhap.getHoten());
        lbtenbang.setText("Danh sách nhân viên");
        nvcontrol.showAllNhanVien(tbdsnv);
    }//GEN-LAST:event_formWindowOpened

    private void tbdsnvMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbdsnvMouseClicked

    }//GEN-LAST:event_tbdsnvMouseClicked

    private void btnnhanvien_isdeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnhanvien_isdeleteActionPerformed
        myFormat.clearTable(this.tbdsnv);
        lbtenbang.setText("Danh sách nhân viên đã bị xóa");
        this.nvcontrol.showAllNhanVien_isdelete(tbdsnv);
    }//GEN-LAST:event_btnnhanvien_isdeleteActionPerformed

    private void itemthem_tai_khoanActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemthem_tai_khoanActionPerformed
        int row = tbdsnv.getSelectedRow();
        if (row != -1) {
            String MSNV = tbdsnv.getValueAt(row, 1).toString();
            f_taikhoan_them f = f_taikhoan_them.getInstance(this.conn, MSNV);
            f.setVisible(true);
        }
    }//GEN-LAST:event_itemthem_tai_khoanActionPerformed

    private void itemthongkeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemthongkeActionPerformed
        try {
            this.genPDF = new GeneratetPDF(this.conn);
            this.genPDF.callOpen(this.genPDF.generatethongke(nvdangnhap.getMSNV()));
        } catch (ParseException ex) {
            Logger.getLogger(f_quanly.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_itemthongkeActionPerformed

    private void itemqlTKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_itemqlTKActionPerformed
        f_taikhoan quantritaikhoan = new f_taikhoan(conn, this.nvdangnhap.getMSNV());
        quantritaikhoan.setVisible(true);
    }//GEN-LAST:event_itemqlTKActionPerformed

    private void search() {
        JOptionPane.showMessageDialog(this, "Gọi hàm tìm kiếm.", "Thông báo", JOptionPane.INFORMATION_MESSAGE);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnnhanvien_isdelete;
    private javax.swing.JButton btnrefresh;
    private javax.swing.JButton btnthem;
    private javax.swing.JButton btntim;
    private javax.swing.JMenuItem itemdangxuat;
    private javax.swing.JMenuItem itemqlTK;
    private javax.swing.JMenuItem itemsua_thongtin_nhanvien;
    private javax.swing.JMenuItem itemthem_tai_khoan;
    private javax.swing.JMenuItem itemthoat;
    private javax.swing.JMenuItem itemthongke;
    private javax.swing.JMenuItem itemthongtin;
    private javax.swing.JMenuItem itemxoa_thongtin_nhanvien;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel lbtenbang;
    private javax.swing.JMenuBar menu;
    private javax.swing.JMenu menuhethong;
    private javax.swing.JMenu menuquanly;
    private javax.swing.JMenu menuttuser;
    private javax.swing.JPanel pnchinh;
    private javax.swing.JPopupMenu popupmenu;
    private javax.swing.JScrollPane spdsnv;
    private javax.swing.JTable tbdsnv;
    private javax.swing.JTextField tftim;
    // End of variables declaration//GEN-END:variables
}
